package com.excesys.exsecs.energy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.energy.entity.WaterHourFlow;
import com.excesys.exsecs.energy.mapper.WaterHourFlowMapper;
import com.excesys.exsecs.energy.service.WaterHourFlowService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 水能耗
 *
 * @author zhangtailong
 * @date 2024-07-03 11:09:53
 */
@Service
public class WaterHourFlowServiceImpl extends ServiceImpl<WaterHourFlowMapper, WaterHourFlow> implements WaterHourFlowService {
    @Override
    public JSONObject serverWaterSurvey(String area) {
        JSONObject result = new JSONObject();//返回数据结构
        //日期时间字符串转换
        DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //当日时间与当月时间
        String startTime = null;//起始时间
        String endTime = null;//结束时间
        //年用水量
        Year toyears = Year.from(LocalDate.now());
        Map toyearsTime = getNewestTime(toyears.toString());
        if (!toyearsTime.isEmpty()) {
            LocalDateTime  startDateTime = LocalDateTime.parse(toyearsTime.get("startTime").toString(), isoFormatter);
            startTime = startDateTime.format(customFormatter);
            LocalDateTime   endDateTime = LocalDateTime.parse(toyearsTime.get("endTime").toString(), isoFormatter);
            endTime = endDateTime.format(customFormatter);
        }
        if (startTime != null && endTime != null){
            double yearsStartSum = this.baseMapper.sumWaterAllNumber("2", startTime,startTime.substring(0, 7).replace("-", ""));
            double yearsEndSum = this.baseMapper.sumWaterAllNumber("2", endTime,endTime.substring(0, 7).replace("-", ""));
            double yearsWater = 0;
            if (yearsStartSum != 0 && yearsEndSum != 0) {
                yearsWater = yearsEndSum - yearsStartSum;
            }
            result.put("yearsWater", yearsWater);
        }else {
            result.put("yearsWater", 0.0);
        }
        //月用水量
        YearMonth tomonth = YearMonth.from(LocalDate.now());
        Map tomonthTime = getNewestTime(tomonth.toString());
        if (!tomonthTime.isEmpty()) {
            LocalDateTime  startDateTime = LocalDateTime.parse(tomonthTime.get("startTime").toString(), isoFormatter);
            startTime = startDateTime.format(customFormatter);
            LocalDateTime   endDateTime = LocalDateTime.parse(tomonthTime.get("endTime").toString(), isoFormatter);
            endTime = endDateTime.format(customFormatter);
        }
        if (startTime != null && endTime != null){
            double tomonthStartSum = this.baseMapper.sumWaterAllNumber("2", startTime,startTime.substring(0, 7).replace("-", ""));
            double tomonthEndSum = this.baseMapper.sumWaterAllNumber("2", endTime,endTime.substring(0, 7).replace("-", ""));
            double tomonthWater = 0;
            if (tomonthStartSum != 0 && tomonthEndSum != 0) {
                tomonthWater = tomonthEndSum - tomonthStartSum;
            }
            result.put("monthWater", tomonthWater);
        }else {
            result.put("monthWater", 0.0);
        }
        //日用水量
        LocalDate today = LocalDate.now();
        Map todayTime = getNewestTime(today.toString());
        if (!todayTime.isEmpty()) {
            LocalDateTime  startDateTime = LocalDateTime.parse(todayTime.get("startTime").toString(), isoFormatter);
            startTime = startDateTime.format(customFormatter);
            LocalDateTime   endDateTime = LocalDateTime.parse(todayTime.get("endTime").toString(), isoFormatter);
            endTime = endDateTime.format(customFormatter);
        }
        if (startTime != null && endTime != null){
            double dayStartSum = this.baseMapper.sumWaterAllNumber("2", startTime,startTime.substring(0, 7).replace("-", ""));
            double dayEndSum = this.baseMapper.sumWaterAllNumber("2", endTime,endTime.substring(0, 7).replace("-", ""));
            double dayWater = 0;
            if (dayStartSum != 0 && dayEndSum != 0) {
                dayWater = dayEndSum - dayStartSum;
            }
            result.put("dayWater", dayWater);
        }else {
            result.put("dayWater", 0.0);
        }
        //总用水量 与 单位面积用水量
        if (endTime != null){
            double totalWater = this.baseMapper.sumWaterAllNumber("2", endTime,endTime.substring(0, 7).replace("-", ""));
            result.put("totalWater", totalWater);
            double areaWater = totalWater / Double.parseDouble(area);
            result.put("areaWater", new BigDecimal(areaWater).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        }else {
            result.put("totalWater", 0.0);
            result.put("areaWater", 0.0);
        }
        return result;
    }

    //获取数据库最新的时间格式 开始时间 与 结束时间
    public Map getNewestTime(String date) {
        Map map = new HashMap();
        //获取当前时间
        WaterHourFlow startHourFlow = this.baseMapper.selectOne(Wrappers.<WaterHourFlow>lambdaQuery()
                .like(WaterHourFlow::getTimeValue, date)
                .orderByAsc(WaterHourFlow::getTimeValue).last("LIMIT 1"));
        //获取上一条数据
        WaterHourFlow endHourFlow = this.baseMapper.selectOne(Wrappers.<WaterHourFlow>lambdaQuery()
                .like(WaterHourFlow::getTimeValue, date)
                .orderByDesc(WaterHourFlow::getTimeValue).last("LIMIT 1"));
        if (ObjectUtils.isNotEmpty(startHourFlow) && ObjectUtils.isNotEmpty(endHourFlow)) {
            map.put("startTime", startHourFlow.getTimeValue().toString());
            map.put("endTime", endHourFlow.getTimeValue().toString());
        }
        return map;
    }
    
}